//
//  RootViewController.m
//  EasyTip
//
//  Created by Jeffrey Palm on 2/14/09.
//  Copyright Jeffrey Palm 2009. All rights reserved.
//

#import "RootViewController.h"
#import "EasyTipAppDelegate.h"
#import "Packet.h"
#import "Category.h"
#import "Criteria.h"
#import "CategoryTableViewController.h"

@interface RootViewController (PrivateMethods)
	- (void)buildStandardPackets;
@end


@implementation RootViewController

@synthesize packets;

- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		self.tableView.delegate = self;
		self.tableView.dataSource = self;
		self.tableView.backgroundColor = [UIColor blackColor];
	}
	return self;
}


// In the future we'll read these from the web or disk, now we'll just build them manually
- (void)buildStandardPackets {
	Packet *p;
	Category *cat;
	
	p = [[Packet alloc] initWithName:@"Standard" pid:@"standard"];
	
	cat = [[Category alloc] initWithName:@"Service Speed"];
	[p add:cat];
	[cat addCriteria:@"Lightning"				value:5 image:@"images/standard/speed/5.png" description:@"Lightning is fast"];
	[cat addCriteria:@"Maglev"					value:4 image:@"images/standard/speed/4.png" description:@"That's a fast motherfucking train!"];
	[cat addCriteria:@"Porsche"					value:3 image:@"images/standard/speed/3.png" description:@"Yes!"];
	[cat addCriteria:@"Michael Phelps"  value:2 image:@"images/standard/speed/2.png" description:@"A pretty fast white dude"];
	[cat addCriteria:@"Turtle"          value:1 image:@"images/standard/speed/1.png" description:@"Turtles aren't fast"];
	
	cat = [[Category alloc] initWithName:@"Food Taste"];
	[p add:cat];
	[cat addCriteria:@"Fit for a King (or Queen)" value:5 image:@"images/standard/taste/5.png" description:@"I had a dream about this food"];
	[cat addCriteria:@"Fine dining" value:4 image:@"images/standard/taste/4.png" description:@"Yuuuuuuuuuuuuuuuummmmmmmmy"];
	[cat addCriteria:@"Olive Garden" value:3 image:@"images/standard/taste/3.png" description:@"At least I got free bread sticks"];
	[cat addCriteria:@"On the toilet all night" value:2 image:@"images/standard/taste/2.png" description:@"There's not enough TP in the world. . ."];
	[cat addCriteria:@"Puking in the bathroom" value:1 image:@"images/standard/taste/1.png" description:@"Uh, yeah"];
	
	cat = [[Category alloc] initWithName:@"Politeness"];
	[p add:cat];
	[cat addCriteria:@"Miss Manners" value:5 image:@"images/standard/politeness/5.png" description:@"Oh my, your politeness gets me downright wet"];
	[cat addCriteria:@"Anne Landers" value:4 image:@"images/standard/politeness/4.png" description:@"If you call me Ruth, I'll break your fucking face"];
	[cat addCriteria:@"Martha Stewart" value:3 image:@"images/standard/politeness/3.png" description:@"What do you mean toss the salad with your hands?"];
	[cat addCriteria:@"Yosemite Sam" value:2 image:@"images/standard/politeness/2.png" description:@"I say, I say, I'm P-O-L-I-T-E; now get over here Miss Martha"];
	[cat addCriteria:@"Anyone from Jersey" value:1 image:@"images/standard/politeness/1.png" description:@"Jersey!"];
	
	cat = [[Category alloc] initWithName:@"Attentiveness"];
	[p add:cat];
	[cat addCriteria:@"At your beckoning call" value:5 image:@"images/standard/attentiveness/5.png" description:@"Whatever you want, you get"];
	[cat addCriteria:@"Timely and watchful" value:4 image:@"images/standard/attentiveness/4.png" description:@"I hover over you like a bumble bee"];
	[cat addCriteria:@"Around" value:3 image:@"images/standard/attentiveness/3.png" description:@"Yeah, I'm here"];
	[cat addCriteria:@"Occasionally make eye contact" value:2 image:@"images/standard/attentiveness/2.png" description:@"What are you looking at?"];
	[cat addCriteria:@"MIA" value:1 image:@"images/standard/attentiveness/1.png" description:@". . ."];
	
	cat = [[Category alloc] initWithName:@"Explanations"];
	[p add:cat];
	[cat addCriteria:@"Georegtown debate captain" value:5 image:@"images/standard/explanations/5.png" description:@"I will give you 174 reasons to order the blooming onion"];
	[cat addCriteria:@"Webster (i.e. the dictionary gal)" value:4 image:@"images/standard/explanations/4.png" description:@"Cibophobia is the fear of food"];
	[cat addCriteria:@"Jimmy Wales" value:3 image:@"images/standard/explanations/3.png" description:@"I'd like a fact check on whether the sirloin is really 'acclaimed'"];
	[cat addCriteria:@"Translated Korean Dance Dance Rev. instructions" value:2 image:@"images/standard/explanations/2.png" description:@"햄버거 좋다"];
	[cat addCriteria:@"W. Bush" value:1 image:@"images/standard/explanations/1.png" description:@"I wonder if it's rude to...wait, what was I saying?"];
	
	[packets addObject:p];
}


- (void)viewDidLoad {
  [super viewDidLoad];
	self.title = @"Select a Scheme";
	self.packets = [NSMutableArray array];
	[self buildStandardPackets];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

// decide what kind of accesory view (to the far right) we will use
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath
{
	return UITableViewCellAccessoryDisclosureIndicator;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [packets count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	int index = [indexPath indexAtPosition:1];
	NSLog(@"[RootViewController] have index %d", index);
	Packet *p = (Packet*)[packets objectAtIndex:index];
	[EasyTipAppDelegate getInstance].currentPacket = p;
	cell.text = p.name;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];

	NSInteger index = [indexPath indexAtPosition:1];
	NSArray *arr = [self packets];
	Packet *p = (Packet*)[arr objectAtIndex:index];
	NSLog(@"Loading packet %@",p.name);
	
	Category *cat = [p firstCategory];
	NSLog(@"Starting with category %@ named %@", cat, cat.name);
	
	CategoryTableViewController *controller = [[CategoryTableViewController alloc] init:cat]; 
	[self.navigationController pushViewController:controller animated:YES];
	[controller release];	
	
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
    [super dealloc];
}


@end

